package com.guigu.thirdpartyservices.config;

import com.aliyun.oss.OSS;
import com.aliyun.oss.OSSClientBuilder;
import com.guigu.thirdpartyservices.propertes.ALiYunProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import lombok.extern.log4j.Log4j2;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;

import javax.annotation.PreDestroy;

@Data
@Configuration
@AllArgsConstructor
@NoArgsConstructor
@Log4j2
public class ALiOssConfig {
    @Autowired
    private ALiYunProperty aliYunProperty;
    private OSS ossClient;

    @Bean
    public OSS ossClient() {
        ossClient= new OSSClientBuilder().build(aliYunProperty.getOss().getEndpoint(), aliYunProperty.getAccessKeyId(),
                aliYunProperty.getAccessKeySecret());
        return ossClient;
    }

    @PreDestroy
    public void onDestroy() {
        this.ossClient.shutdown();
    }
}
